package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.dao.ComputerSubsidyDao;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.salary.ComputerSubsidyEntity;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.salary.ComputerSubsidyService;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RestController
public class ComputerSubsidyServiceImpl extends ServiceImpl<ComputerSubsidyDao, ComputerSubsidyEntity> implements ComputerSubsidyService {

    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private JobController jobController;
    @Override
    public PageResult queryPage(@RequestParam(value = "oid", required = false) String oid,
                                @RequestParam(value = "equipment", required = false) List<Integer> equipment,
                                @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                @RequestParam(value = "code", required = false) String code,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "orgName", required = false) String orgName,
                                @RequestParam(value = "unitName", required = false) String unitName,
                                @RequestParam(value = "jobName", required = false) String jobName,
                                @RequestParam(value = "applyDateStart", required = false) String applyDateStart,
                                @RequestParam(value = "applyDateEnd", required = false) String applyDateEnd,
                                @RequestParam(value = "approveStatus", required = false) List<Integer> approveStatus,
                                @RequestParam(value = "type", required = false) List<Integer> type,
                                @RequestParam(value = "status", defaultValue = "1") Integer status,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ComputerSubsidyEntity> page = this.page(
                new Page<ComputerSubsidyEntity>(pageNum,pageSize),
                new QueryWrapper<ComputerSubsidyEntity>()
                        .eq(!StringUtils.isEmpty(oid),"oid",oid)
                        .in(!CollectionUtils.isEmpty(equipment),"equipment",equipment)
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq(StringUtils.isEmpty(oid)&&CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .like(!StringUtils.isEmpty(code),"code",code)
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .like(!StringUtils.isEmpty(orgName),"org_name",orgName)
                        .like(!StringUtils.isEmpty(unitName),"unit_name",unitName)
                        .like(!StringUtils.isEmpty(jobName),"job_name",jobName)
                        .ge(!com.zhangtai.utils.StringUtils.isEmpty(applyDateStart),"apply_date",applyDateStart+" 00:00:00")
                        .le(!com.zhangtai.utils.StringUtils.isEmpty(applyDateEnd),"apply_date",applyDateEnd+" 29:59:59")
                        .in(!CollectionUtils.isEmpty(approveStatus),"approve_status",approveStatus)
                        .in(!CollectionUtils.isEmpty(type),"type",type)
                        .eq( status!=null,"status",status)
                        .eq("is_delete",0).orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public R saveComputerSubsidy(Date applyDate,
                                 Long oid,
                                 Date beforeSubsidyEnd,
                                 Long beforeSubsidyId,
                                 BigDecimal afterSubsidy,
                                 Date afterSubsidyStart,
                                 Date afterSubsidyEnd,
                                 String fileUrl,
                                 String bpmUrl,
                                 String reason) {
        if(beforeSubsidyId!=null){
            ComputerSubsidyEntity computerSubsidyEntity=getByIdE(beforeSubsidyId);
            if (!ObjectUtils.isEmpty(computerSubsidyEntity)&&beforeSubsidyEnd!=null){
                computerSubsidyEntity.setEndDate(beforeSubsidyEnd);
                computerSubsidyEntity.setStatus(0);
               updateByIdE(computerSubsidyEntity);
            }
        }
        if(afterSubsidy==null||afterSubsidyStart==null||afterSubsidyEnd==null){
            return R.ok();
        }
       EmployeeInfoEntity employeeInfoEntity= employeeInfoService.getByIdE(oid);
        if(!ObjectUtils.isEmpty(employeeInfoEntity)){
            ComputerSubsidyEntity entity= new ComputerSubsidyEntity();
            entity.setType(3);
            entity.setEndDate(afterSubsidyEnd);
            entity.setEquipment(2);
            entity.setFileUrl(fileUrl);
            entity.setApplyDate(applyDate );
            entity.setJobId(employeeInfoEntity.getJobId());
            entity.setUnitId(employeeInfoEntity.getUnitId());
            entity.setOrgId(employeeInfoEntity.getOrgId());
            entity.setJobName(jobController.getJobName(employeeInfoEntity.getJobId()));
            entity.setOrgName(jobController.getOrgNameById(employeeInfoEntity.getOrgId()));
            entity.setUnitName(jobController.getOrgNameById(employeeInfoEntity.getUnitId()));
            entity.setCode(employeeInfoEntity.getCode());
            entity.setName(employeeInfoEntity.getName());
            entity.setOid(employeeInfoEntity.getOid());
            entity.setApproveStatus(2);
            entity.setBpmUrl(bpmUrl);
            entity.setApplyMode(1);
            entity.setCreateTime(new Date());
            entity.setMonthlyAmount(afterSubsidy);
            entity.setDuration(DateUtils.monthsBetween(afterSubsidyStart,afterSubsidyEnd));
            entity.setReason(reason);
            entity.setStartDate(afterSubsidyStart);
            saveE(entity);
        }
        return R.ok();
    }


    @Override
    public ComputerSubsidyEntity getByIdE(Long id) {
            ComputerSubsidyEntity one = this.getOne(new QueryWrapper<ComputerSubsidyEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(ComputerSubsidyEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(ComputerSubsidyEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            ComputerSubsidyEntity entity = new ComputerSubsidyEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}